package org.mobilesynergies.epic.client.parameter;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.jivesoftware.smackx.FormField;


public class BooleanParameter extends Parameter {

	private boolean mValue = false;

	public BooleanParameter(boolean value) {
		mValue = value;
	}

	public BooleanParameter(HashMap<String, String> map) {
		if(map.containsKey(KEY_VALUE)){
			try{
				//accept "true" and "1" strings to represent boolean true
				String value = map.get(KEY_VALUE);
				mValue = Boolean.parseBoolean(value) || value.equalsIgnoreCase("1");				
			} catch (Exception e){
				System.out.println("Unable to parse value");
			}
		}
	}
	

	public boolean getValue() {
		return mValue;
	}
	
	@Override
	public String getValueAsString() {
		return Boolean.toString(getValue());
	}
	
	public void setValue(boolean value) {
		mValue = value;
	}

	@Override
	public String getType() {
		return Parameter.TYPENAME_BOOLEAN;
	}

	@Override
	public String toXml(String name) {
		String type = Boolean.class.getCanonicalName();
		String value = Boolean.toString(mValue); 
		String extraattributes = null;
		String extraxml = null;
		return toXml(name, type, value, extraattributes, extraxml);
	}
	
	@Override
	public Set<FormField> toFormFields(String name) {
		Set<FormField> set = new HashSet<FormField>();
		FormField f1 = new FormField(name);
		f1.setLabel(name);
		f1.setType(FormField.TYPE_BOOLEAN);
		f1.addValue(Boolean.toString(mValue));
		set.add(f1);
		
		FormField f2 = new FormField(name+SPECIALIZECHARACTER+KEY_TYPE);
		f2.setLabel(name);
		f2.setType(FormField.TYPE_HIDDEN);
		f2.addValue(TYPENAME_BOOLEAN);
		set.add(f2);		
		
		
		return set;
	}

}
